package controll

import (
	"SalesMangent/function"
	"SalesMangent/models"
	"fmt"

	"github.com/gin-gonic/gin"
)

func GetO_detail(c *gin.Context) {
	db := function.GetDB()
	var o_detaildata []models.O_detail
	db.Table("o_detail").Select("d_code, o_number, f_code, i_code, d_number, d_money").Order("d_code ASC").Find(&o_detaildata)
	c.JSON(200, o_detaildata)
}

func DelO_detail(c *gin.Context) {
	db := function.GetDB()
	requesto_detail := make(map[string]interface{})
	var o_detaildata models.O_detail
	c.ShouldBind(&requesto_detail)
	o_detaildata.Dcode = requesto_detail["Dcode"].(string)
	db.Table("o_detail").Where("d_code = ?", o_detaildata.Dcode).Delete(&o_detaildata)
	c.JSON(200, gin.H{
		"code": 200,
	})
}

func AddO_detail(c *gin.Context) {
	db := function.GetDB()
	requesto_detail := make(map[string]interface{})
	c.ShouldBind(&requesto_detail)
	fmt.Println(requesto_detail)
	var o_detaildata models.O_detail
	o_detaildata.Dcode = requesto_detail["Acode"].(string)
	o_detaildata.Onumber = requesto_detail["Anumber"].(string)
	o_detaildata.Fcode = requesto_detail["Afcode"].(string)
	o_detaildata.Icode = requesto_detail["Aicode"].(string)
	db.Table("o_detail").Select("d_code", "o_number", "f_code", "i_code", "d_number", "d_money").Create(&o_detaildata)
	c.JSON(200, gin.H{
		"code":     200,
	})
}

func UpdateO_detail(c *gin.Context) {
	db := function.GetDB()
	requesto_detail := make(map[string]interface{})
	c.ShouldBind(&requesto_detail)
	var o_detaildata models.O_detail
	fmt.Println(requesto_detail)
	o_detaildata.Dcode = requesto_detail["Ucode"].(string)
	o_detaildata.Onumber = requesto_detail["Unumber"].(string)
	o_detaildata.Fcode = requesto_detail["Ufcode"].(string)
	o_detaildata.Icode = requesto_detail["Uicode"].(string)
	o_detaildata.Dnumber = requesto_detail["Udnumber"].(string)
	o_detaildata.Dmoney = requesto_detail["Udmoney"].(string)
	db.Table("o_detail").Where("d_code = ?", o_detaildata.Dcode).Updates(&o_detaildata)
	c.JSON(200, gin.H{
		"code": 200,
	})
}
